package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.POJO.Users;
import com.qf.mapper.UsersMapper;
import com.qf.service.UserService;
import com.qf.utils.JWTUtils;
import com.qf.utils.R;
import com.qf.utils.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.security.PrivateKey;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private BCryptPasswordEncoder encoder;

    @Override
    public R checkLogin(Users users) {
        if (users ==null){
            return R.fail().desc("非法参数").code("1");
        }
        //根据用户名查询用户信息
        Users tbUsers = usersMapper.selectByUserName(users.getUserName());
        if (tbUsers == null){
            return R.fail().desc("用户名密码错误").code("1");
        }
        boolean matches = encoder.matches(users.getUserPwd(), tbUsers.getUserPwd());
        if (!matches){
            return R.fail().desc("用户名密码错误").code("1");
        }


        List<String> permissions= usersMapper.selectPermsByUsername(tbUsers.getUserName());
        List<String>roles= usersMapper.selectRolesByUsername(tbUsers.getUserName());
        String perStr =permissions.stream().collect(Collectors.joining(","));
        String roleStr =roles.stream().collect(Collectors.joining(","));

        tbUsers.setPerStr(perStr);
        tbUsers.setRoleStr(roleStr);
        PrivateKey privateKey = null;
        try {
            //获取私钥
            byte[] keyBytes = RsaUtils.readFile("pri.txt");
            privateKey=RsaUtils.getPrivateKey(keyBytes);
//            privateKey =
//                    RsaUtils.getPrivateKey(ResourceUtils.getFile("classpath:pri.txt").getPath());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        String jwtToken = JWTUtils.generateTokenExpireInMinutes(tbUsers, privateKey, 30);
        return R.ok().code("0").data(jwtToken);

    }

    @Override
    public R register(Users users) {
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("user_name", users.getUserName());
        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("phone_number", users.getPhoneNumber());
        if (users==null) {
            return R.fail().desc("账号信息不能为空，请修改");
        }
        if (usersMapper.selectOne(wrapper1) != null || usersMapper.selectOne(wrapper2) != null){
            return R.fail().desc("账号信息已经存在，请修改");
        }
        users.setCreateTime(new Date());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(users.getUserPwd());
        users.setUserPwd(hashedPassword);
        int insert = usersMapper.insert(users);
        if (insert<=0){
            return R.fail().desc("注册失败");
        }
        return R.ok().desc("注册成功");
    }
}
